package com.example.bilibiliprojectspringboot.service.impl;

import com.example.bilibiliprojectspringboot.mapper.NavMapper;
import com.example.bilibiliprojectspringboot.model.entity.NavInfo;
import com.example.bilibiliprojectspringboot.service.NavService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NavServiceImpl implements NavService {

  @Autowired
  NavMapper navMapper;

  // 获取nav列表，父子nav按照id 以及 uid 的关系组合
  @Override
  public List<NavInfo> selectAll() {
    List<NavInfo> list = navMapper.selectAll();
    List<NavInfo> reList = new ArrayList<>();
    list.forEach(nav -> {
      if(nav.getPId() == -1){
        reList.add(nav);
      }else{
        NavInfo temp = reList.get(nav.getPId());
        if(temp.getItems() == null){
          temp.setItems(new ArrayList<>());
          temp.getItems().add(nav);
        }else{
          temp.getItems().add(nav);
        }
      }
    });
    return reList;
  }

}
